<?php

class Utils
{
  static public function slugify($text) {
    $text = Utils::removeAccents($text);
    // replace all non letters or digits by -
    $text = preg_replace('/\W+/', '-', $text);

    // trim and lowercase
    $text = strtolower(trim($text, '-'));

    return $text;
  }


  public static function removeAccents($text) {
    $text = str_replace(
        array( "á", "Á", "â", "Â", "à", "À", "å", "Å", "ã", "Ã", "ä", "Ä", "æ", "Æ", "ç", "Ç", "é", "É", "ê", "Ê", "è", "È", "ë", "Ë", "í", "Í", "î", "Î", "ì", "Ì", "ï", "Ï", "ñ", "Ñ", "ó", "Ó", "ô", "Ô", "ò", "Ò", "ø", "Ø", "õ", "Õ", "ö", "Ö", "", "", "", "", "ú", "Ú", "û", "Û", "ù", "Ù", "ü", "Ü", "ý", "Ý", "ÿ", "" ),
        array( "a", "A", "a", "A", "a", "A", "a", "A", "a", "A", "a", "A", "ae", "AE", "c", "C", "e", "E", "e", "E", "e", "E", "e", "E", "i", "I", "i", "I", "i", "I", "i", "I", "n", "N", "o", "O", "o", "O", "o", "O", "o", "O", "o", "O", "o", "O", "oe", "OE", "s", "S", "u", "U", "u", "U", "u", "U", "u", "U", "y", "Y", "y", "Y" ),
        $text
       );
    return $text;
  }

  public static function setSlogTitle($text, $response){
      $response->setSlot('title', '<title>' . $text . ' - CPF Aizenay</title>');
  }

  public static function slugDescription($text){
      return substr(strip_tags($text), 0, 250);
  }

  public static function getImageSize($folderPath, $sfAsset, $widthContainer, $heightContainer){

      $imagesAttributes = null;

      if(!empty($sfAsset[ 'filename' ])){

          $width = $sfAsset['large_width'];
          $height = $sfAsset['large_height'];
          $url = '/' . $folderPath . '/thumbnail/large_' . $sfAsset[ 'filename' ];

          if($sfAsset['large_width'] > $sfAsset['original_width'] && $sfAsset['large_height'] > $sfAsset['original_height']){
            $width = $sfAsset['original_width'];
            $height = $sfAsset['original_height'];
            $url = '/' . $folderPath . '/' . $sfAsset[ 'filename' ];
          }

          $marginTop = $height < $heightContainer ? intval(($heightContainer - $height)/2) : 0;
          $marginLeft = $width < $widthContainer ? intval(($widthContainer - $width)/2) : 0;

          $imagesAttributes = array(
            'optimal' => array('url' => $url, 'width' => $width, 'height' => $height,
                               'marginTop' => $marginTop, 'marginLeft' => $marginLeft),
            'full' => array('url' => '/' . $folderPath . '/' . $sfAsset[ 'filename' ], 'width' => $sfAsset['original_width'], 'height' => $sfAsset['original_height'],
                            'marginTop' => $sfAsset['original_height'] < $heightContainer ? intval(($heightContainer - $sfAsset['original_height'])/2) : 0,
                            'marginLeft' => $sfAsset['original_width'] < $widthContainer ? intval(($widthContainer - $sfAsset['original_width'])/2) : 0),
            'large' => array('url' => '/' . $folderPath . '/thumbnail/large_' . $sfAsset[ 'filename' ], 'width' => $sfAsset['large_width'], 'height' => $sfAsset['large_height'],
                             'marginTop' => $sfAsset['large_height'] < $heightContainer ? intval(($heightContainer - $sfAsset['large_height'])/2) : 0,
                             'marginLeft' => $sfAsset['large_width'] < $widthContainer ? intval(($widthContainer - $sfAsset['large_width'])/2) : 0),
            'small' => array('url' => '/' . $folderPath . '/thumbnail/small_' . $sfAsset[ 'filename' ], 'width' => $sfAsset['small_width'], 'height' => $sfAsset['small_height'],
                             'marginTop' => $sfAsset['small_height'] < $heightContainer ? intval(($heightContainer - $sfAsset['small_height'])/2) : 0,
                             'marginLeft' => $sfAsset['small_width'] < $widthContainer ? intval(($widthContainer - $sfAsset['small_width'])/2) : 0)
          );
      }

      return $imagesAttributes;
  }

}

?>
